import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * 
 * Gui class for adding a review by reader
 *
 */
public class AddReviewGui extends JPanel  {

	private static final long serialVersionUID = 1L;
	/** add button, for adding a reader review */
	private JButton add = null;
	/** book name label */
	private JLabel book=null;
	/** text area for the review */
	public JTextArea reviewText = null;
	/** check box for signature */
	private JCheckBox signatureOk = null;
	/** signature label */
	private JLabel signature = null;
	/** reader's name label */
	private JLabel writerName = null;
	/** readr's name */
	private JTextField userName = null;
	/** books list */
	public JComboBox bookName = null;  //  @jve:decl-index=0:visual-constraint="299,56"
	/** cancel button, go back to main screen */
	private JButton cancel = null;  //  @jve:decl-index=0:visual-constraint="641,226"
	/** window panel */
	public JPanel panel=new JPanel();  //  @jve:decl-index=0:visual-constraint="139,10"
	/** scroll panel for the review text */
	private JScrollPane jscroll;
	/** check if the user added a signature */
	public boolean isChecked=false;
	private JButton exit = null;
	
	/**
	 * This is the default constructor
	 */
	public AddReviewGui() {
		super();
		initialize();
	}

	/**
	 * initializes the Gui Class
	 */
	private void initialize() {
		writerName = new JLabel("Name");
		writerName.setBounds(new Rectangle(60, 92, 72, 19));
	
		signature = new JLabel("Signature");
		signature.setBounds(new Rectangle(347, 90, 69, 23));
		
		book = new JLabel("Book Name:");
		book.setBounds(new Rectangle(20, 40, 90, 23));
		Main.addReview.retreive();
		panel.setLayout(null);
		panel.add(getJScroll(),null);
		panel.add(getAdd(), null);
		panel.add(getSignatureOk(), null);
		panel.add(signature, null);
		panel.add(writerName, null);
		panel.add(getUserName(), null);
		panel.add(getBookName(),null);
		panel.add(getExit(),null);
		panel.add(book);
		panel.setEnabled(false);
		panel.setSize(new Dimension(578, 382));
		panel.setVisible(false);
		panel.add(getExit2(), null);
	}
	
	/**
	 * This method initializes getJScroll and set Viewportview on reviewText Text Area
	 * 	
	 * @return javax.swing.JScrollPane	
	 */

	private JScrollPane getJScroll() {
		if (jscroll == null) {
			jscroll = new JScrollPane();
			jscroll.setBounds(new Rectangle(59, 140, 408, 230));
			jscroll.setViewportView(getReviewText());
		}
		return jscroll;
	}
	
	/**
	 * This method initializes getBookName, the combo box initialized according to the books purchased by the reader
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	
	private JComboBox getBookName()
	{
		if(bookName==null)
		{
			if(Main.addReview.res!=null)
				bookName=new JComboBox(Main.addReview.res);
			else
				bookName=new JComboBox();
			bookName.setBounds(102, 40, 350, 20);
		}
		return bookName;
	}
	
	/**
	 * This method initializes getAdd and set listener on this button
	 * 	
	 * @return javax.swing.JButton	
	 */
	
	private JButton getAdd() {
		if (add == null) {
			add = new JButton("Add");
			add.setBounds(new Rectangle(477, 47, 92, 31));
			add.setEnabled(false);
			add.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Main.addReview.addreview();
				}
			});
		}
		return add;
	}
	
	/**
	 * This method initializes getExit and set listener on this button
	 * 	
	 * @return javax.swing.JButton	
	 */
	
	private JButton getExit() {
		if (cancel == null) {
			cancel = new JButton("Back");
			cancel.setBounds(new Rectangle(477, 91, 90, 31));
			cancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					Main.addReview.back();
				}
			});
		}
		return cancel;
	}

	/**
	 * This method initializes reviewText and set listener on this button
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getReviewText() {
		if (reviewText == null) {
			reviewText = new JTextArea();
			reviewText.setBounds(new Rectangle(59, 140, 408, 230));
			reviewText.setBorder(BorderFactory.createLineBorder(Color.black));
			reviewText.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyPressed(java.awt.event.KeyEvent e) {
					if(reviewText.getText().length()>0 && isChecked) add.setEnabled(true);
					else add.setEnabled(false);
				}
			});
		}
		return reviewText;
	}

	/**
	 * This method initializes signatureOk	and set listener on this button. this check box, indicates if a signature was added
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getSignatureOk() {
		if (signatureOk == null) {
			signatureOk = new JCheckBox();
			signatureOk.setBounds(new Rectangle(323, 85, 19, 33));
			signatureOk.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(isChecked)
					{
						isChecked=false;
						add.setEnabled(false);
						
					}
					else
					{
						isChecked=true;
						if(reviewText.getText().length()>0) add.setEnabled(true);
					}
						
					}
				}
			);
		}
		return signatureOk;
	}

	/**
	 * This method initializes userName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getUserName() {
		if (userName == null) {
			userName = new JTextField();
			userName.setBounds(new Rectangle(100, 92, 200, 20));
			userName.setText(Main.gusers.name);
			userName.setEnabled(false);
		}
		return userName;
	}

	/**
	 * This method initializes exit	and adds a listener
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getExit2() {
		if (exit == null) {
			exit = new JButton("Logout");
			exit.setBounds(new Rectangle(479, 6, 89, 34));
			exit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Main.addReview.exit();
				}
			});
		}
		return exit;
	}

	
}  //  @jve:decl-index=0:visual-constraint="129,9"
